#!/usr/bin/env python
################################################################################
#            __  __            _   ____        _ _ ____        _              #
#           |  \/  | ___  __ _| |_| __ )  __ _| | | __ )  ___ | |_           #
#           | |\/| |/ _ \/ _` | __|  _ \ / _` | | |  _ \ / _ \| __|         #
#           | |  | |  __/ (_| | |_| |_) | (_| | | | |_) | (_) | |_          #
#           |_|  |_|\___|\__,_|\__|____/ \__,_|_|_|____/ \___/ \__|          #
#                                                                             #
#                                     by                                       #
# _______ .______       _______  _______   ______   ______   _______   _______  #
#|   ____||   _  \     |   ____||   ____| /      | /  __  \ |       \ |   ____|  #
#|  |__   |  |_)  |    |  |__   |  |__   |  ,----'|  |  |  ||  .--.  ||  |__      #
#|   __|  |      /     |   __|  |   __|  |  |     |  |  |  ||  |  |  ||   __|      #
#|  |     |  |\  \----.|  |____ |  |____ |  `----.|  `--'  ||  '--'  ||  |____      #
#|__|     | _| `._____||_______||_______| \______| \______/ |_______/ |_______|    #
#                                                                                 #
#                           Connection Core-Module                               #
#                             Modular FrameWork                                 #
#
# Coders
#
#	Justin Giorgi (rebel_kid)
#
# Debuggers
#
#	Justin Giorgi (rebel_kid)
#	Lachlan Gilmour (chilli0)
#	David Liedle (spiderbyte)
#	(Polarina)
#	(prologic)
#
################################################################################
#                                                                              #
#                               GPL Licensed                                   #
#                                                                              #
################################################################################

#Imports
from time import sleep

# Connects and joins
def connect():
	connectserv(globalvars)
	connectchan(globalvars)

# Connects to networks
def connectserv(globalvars):
	globalvars["cursor"].execute("select netaddy from networks where active = 1")
	data = globalvars["cursor"].fetchall()
	index = 1
	for row in data:
		server = irc.server()
		globalvars["servers"].append(server.connect(row[0], port, globalvars["mynick"], globalvars["password"]))
			
# Joins channels
def connectchan(globalvars):
	globalvars["cursor"].execute("select channame, netid, id from channels where active = 1")
	data = globalvars["cursor"].fetchall()
	for row in data:
		net = globalvars["servers"][row[1] - 1]
		net.join(row[0])
		channames = ChanNames(globalvars, net, row[0], row[2])

class ChanNames:

	def __init__(self, globalvars, server, channel, chanid):
		if (server, "names") not in globalvars["special"]:
			globalvars["special"][(server, "namreply")] = self
			self.chanid = chanid
			server.names(channel)
		else:
			sleep(1)
			self.__init__(globalvars, server, channel)
	def reply(self, globalvars, server, event):
		names = event.arguments()[2].replace("+", "").replace("@", "").replace("%", "").replace("&", "").replace("~", "").split(' ')
		globalvars["channelnames"][(server, self.chanid)] = names
				
def autoload(globalvars):
	
	# Loads connect
	globalvars["connect"] = connect
	
def load(globalvars, msgvars):
	
	# Loads connect
	globalvars["connect"] = connect
	return True
			
def unload(globalvars, msgvars, reloadmodule = False):
	
	# Does Nothing
	if reloadmodule:
		pass
		
	else:
		msgvars["server"].privmsg(msgvars["destination"], "You want to unload my what?!?! How bout I just remove your liver.")
		return False
